package com.wpre.lotterysystem.service.activitystatus.operator;

import com.wpre.lotterysystem.dao.dataobject.ActivityPrizeDO;
import com.wpre.lotterysystem.dao.mapper.ActivityPrizeMapper;
import com.wpre.lotterysystem.service.dto.ConvertActivityStatusDTO;
import com.wpre.lotterysystem.service.enums.ActivityPrizeStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LianBao
 * Date: 2024-12-24
 * Time: 20:04
 */
@Component
public class PrizeOperator extends AbstractActivityOperator {

    @Autowired
    private ActivityPrizeMapper activityPrizeMapper;

    @Override
    public Integer sequence() {
        return 1;
    }

    @Override
    public Boolean needConvert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        //判断是否需要转换
        if (convertActivityStatusDTO.getActivityId() == null
                || convertActivityStatusDTO.getTargetActivityStatus() == null) {
            return false;
        }
        //根据活动id和奖品id查
        ActivityPrizeDO activityPrizeDO = activityPrizeMapper.selectByAidPid(convertActivityStatusDTO.getActivityId()
                , convertActivityStatusDTO.getPrizeId());

        if (activityPrizeDO == null) {
            return false;
        }
        //如果数据库中的状态和参数传递的状态一样则不转换
        if (activityPrizeDO.getStatus().equalsIgnoreCase(convertActivityStatusDTO.getUserStatus().name())) {
            return false;
        }
        return true;

    }

    @Override
    public Boolean convert(ConvertActivityStatusDTO convertActivityStatusDTO) {

        try {
            activityPrizeMapper.updateStatus(convertActivityStatusDTO.getActivityId(),
                    convertActivityStatusDTO.getPrizeId(),
                    convertActivityStatusDTO.getTargetPrizeStatus().name());
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
